package com.cennavi.minenavidemo.utils;

import android.content.Context;
import android.graphics.Typeface;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SpannableBuilder {

    private Context context;
    private List<SpanWrapper> list;

    private SpannableBuilder(Context context) {
        this.context = context;
        this.list = new ArrayList<>();
    }

    public SpannableBuilder append(String text, int textSize, int textColor) {
        return append(text, textSize, textColor, false);
    }

    public SpannableBuilder append(String text, int textSize, int textColor, boolean isBold) {
        list.add(new SpanWrapper(text, textSize, textColor, isBold));
        return this;
    }

    public Spannable build() {
        SpannableStringBuilder textSpan = new SpannableStringBuilder();

        int start = 0;
        int end = 0;

        for (int i = 0; i < list.size(); i++) {
            SpanWrapper wrapper = list.get(i);
            String text = wrapper.getText();
            start = end;
            end = end + text.length();
            textSpan.append(text);

            AbsoluteSizeSpan sizeSpan = new AbsoluteSizeSpan(getContext().getResources().getDimensionPixelSize(wrapper.getTextSize()));
            ForegroundColorSpan colorSpan = new ForegroundColorSpan(getContext().getResources().getColor(wrapper.getTextColor()));
            if (wrapper.isBold) {
                textSpan.setSpan(new StyleSpan(Typeface.BOLD), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            }

            textSpan.setSpan(sizeSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            textSpan.setSpan(colorSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return textSpan;
    }

    public static SpannableBuilder create(Context context) {
        return new SpannableBuilder(context);
    }

    public Context getContext() {
        return context;
    }

    private class SpanWrapper {
        String text;
        int textSize;
        int textColor;
        boolean isBold;

        public SpanWrapper(String text, int textSize, int textColor, boolean isBold) {
            this.text = text;
            this.textSize = textSize;
            this.textColor = textColor;
            this.isBold = isBold;
        }

        public String getText() {
            return text;
        }

        public void setText(String text) {
            this.text = text;
        }

        public int getTextSize() {
            return textSize;
        }

        public void setTextSize(int textSize) {
            this.textSize = textSize;
        }

        public int getTextColor() {
            return textColor;
        }

        public void setTextColor(int textColor) {
            this.textColor = textColor;
        }

        public boolean isBold() {
            return isBold;
        }

        public void setBold(boolean bold) {
            isBold = bold;
        }
    }

    public static SpannableString matcherSearchText(int color, String text, String keyword) {
        SpannableString ss = new SpannableString(text);
        Pattern pattern = Pattern.compile(keyword);
        Matcher matcher = pattern.matcher(ss);
        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            ss.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        }
        return ss;
    }

    /**
     * 高亮显示关键字
     *
     * @param text    文本
     * @param keyword 关键字
     * @param color   关键字高亮颜色
     */
    public static SpannableString highlightKeyword(CharSequence text, String keyword, int color) {
        return highlightKeyword(text, keyword, color, true);
    }

    /**
     * 高亮显示关键字
     *
     * @param text            文本
     * @param keyword         关键字
     * @param color           关键字高亮颜色
     * @param caseInsensitive 是否忽略大小写
     */
    public static SpannableString highlightKeyword(CharSequence text, String keyword, int color, boolean caseInsensitive) {
        SpannableString ss = new SpannableString(text);
        // 转换后可以把特殊符号变成普通字符串，防止正则表达式出现问题
        keyword = Pattern.quote(keyword);
        Pattern pattern = caseInsensitive ? Pattern.compile(keyword, Pattern.CASE_INSENSITIVE) : Pattern.compile(keyword);
        Matcher matcher = pattern.matcher(ss);
        while (matcher.find()) {
            int start = matcher.start();
            int end = matcher.end();
            ss.setSpan(new ForegroundColorSpan(color), start, end, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
        }
        return ss;
    }

    public void clear() {
        list.clear();
    }

}

